package com.qianya.oa.controller;

import com.qianya.oa.common.annotation.Log;
import com.qianya.oa.common.core.controller.BaseController;
import com.qianya.oa.common.core.domain.AjaxResult;
import com.qianya.oa.common.core.domain.entity.SysDept;
import com.qianya.oa.common.core.domain.entity.SysUser;
import com.qianya.oa.common.core.page.TableDataInfo;
import com.qianya.oa.common.enums.BusinessType;
import com.qianya.oa.common.utils.poi.ExcelUtil;
import com.qianya.oa.domain.Outside;
import com.qianya.oa.vo.OutsideVo;
import com.qianya.oa.service.IOutsideService;
import com.qianya.oa.service.ISysDeptService;
import com.qianya.oa.service.SysUserService;
import com.qianya.oa.util.CommonResult;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 【 出差】Controller
 * 
 * @author qianya
 * @date 2023-05-30
 */
@Controller
@RequestMapping("/outside")
public class OutsideController extends BaseController
{
    private final String prefix = "activiti/outside";

    @Autowired
    private IOutsideService outsideService;
    @Autowired
    private SysUserService userService;
    @Autowired
    private ISysDeptService deptService;
    @Resource
    private TaskService taskService;
    @Resource
    private RuntimeService runtimeService;


    @GetMapping()
    public String outside()
    {
        return prefix + "/outside";
    }

    /**
     * 查询【 出差】列表
     */

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Outside outside) {
        SysUser user = getSysUser();
        String username = user.getLoginName();
        outside.setSysUserName(username);
        outside.setSysUserid(user.getUserId());
        startPage();
        List<OutsideVo> list = outsideService.selectOutsideList(outside);
        return getDataTable(list);
    }

    /**
     * 导出【 出差】列表
     */

    @Log(title = "【 出差】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Outside outside)
    {
        List<OutsideVo> list = outsideService.selectOutsideList(outside);
        ExcelUtil<OutsideVo> util = new ExcelUtil<OutsideVo>(OutsideVo.class);
        return util.exportExcel(list, "【 出差】数据");
    }

    /**
     * 新增【 出差】
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        SysUser user = getSysUser();
        SysDept sysDept = deptService.selectDeptById(user.getDeptId());
        mmap.put("user", user);
        mmap.put("dept", sysDept);
        mmap.put("userlist", userService.selectLeaderListFirst(Math.toIntExact(sysDept.getDeptId()),"部门经理"));
        return prefix + "/add";
    }

    /**
     * 新增保存【 出差】
     */
    @Log(title = "【 出差】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Outside outside) {
        SysUser user = getSysUser();
        return toAjax(outsideService.insertOutside(outside,user));
    }

    /**
     * 修改【 出差】
     */

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        OutsideVo outside = outsideService.selectOutsideById(id);
        mmap.put("outside", outside);
        return prefix + "/edit";
    }

    /**
     * 修改保存【 出差】
     */

    @Log(title = "【 出差】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Outside outside)
    {
        return toAjax(outsideService.updateOutside(outside));
    }

    /**
     * 删除【 出差】
     */

    @Log(title = "【 出差】", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(outsideService.deleteOutsideByIds(ids));
    }







    /**
     * 部门领导审批
     * @return
     */
    @GetMapping("/deptleadercheck")
    public String deptleadercheck(String taskid, ModelMap mmap) {
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
            OutsideVo outside = outsideService.selectOutsideById(Long.parseLong(p.getBusinessKey()));
            mmap.put("outside", outside);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("createTime", sdf.format(outside.getCreateTime()));
            mmap.put("taskid", taskid);
            mmap.put("userlist", userService.selectLeaderListFirst(null,"总经理"));
        }
        return prefix + "/deptleadercheck";
    }

    /**
     * 人事审批
     * @return
     */
    @GetMapping("/hrcheck")
    public String hrcheck(String taskid, ModelMap mmap)
    {
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
            OutsideVo outside = outsideService.selectOutsideById(Long.parseLong(p.getBusinessKey()));
            mmap.put("outside", outside);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("createTime", sdf.format(outside.getCreateTime()));
            mmap.put("taskid", taskid);
        }
        return prefix + "/hrcheck";
    }



    /**
     * 调整申请
     * @return
     */
    @GetMapping("/modifyapply")
    public String modifyapply(String taskid, ModelMap mmap)
    {
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
            OutsideVo outside = outsideService.selectOutsideById(Long.parseLong(p.getBusinessKey()));
            mmap.put("outside", outside);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("createTime", sdf.format(outside.getCreateTime()));
            mmap.put("taskid", taskid);
        }
        return prefix + "/modifyapply";
    }

    /**
     * 发起请假申请
     * 驳回后使用
     * @return
     */
    @GetMapping("/addoutside")
    public String addoutside(String taskid, ModelMap mmap)
    {
        Task t = taskService.createTaskQuery().taskId(taskid).singleResult();
        String processId = t.getProcessInstanceId();
        ProcessInstance p = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
        if (p != null) {
            SysUser user = getSysUser();
            SysDept sysDept = deptService.selectDeptById(user.getDeptId());
            mmap.put("user", user);
            mmap.put("dept", sysDept);
            mmap.put("userlist", userService.selectLeaderListFirst(Math.toIntExact(sysDept.getDeptId()),"部门经理"));
            OutsideVo outside = outsideService.selectOutsideById(Long.parseLong(p.getBusinessKey()));
            mmap.put("outside", outside);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mmap.put("createTime", sdf.format(outside.getCreateTime()));
            mmap.put("taskid", taskid);
        }
        return prefix + "/addoutside";
    }


//    -----***----------------新版分割线-----------------------------

    /**
     * 查询【 出差】列表
     */

    @PostMapping("/listVue")
    @ResponseBody
    public CommonResult<?> listVue(Outside outside) {
        SysUser user = getSysUser();
        String username = user.getLoginName();
        outside.setSysUserName(username);
        outside.setSysUserid(user.getUserId());
        startPage();
        List<OutsideVo> list = outsideService.selectOutsideList(outside);
        return CommonResult.success(getDataTable(list));
    }

    /**
     * 获取出差数据
     */
    @GetMapping("/getAddInfo")
    @ResponseBody
    public CommonResult<?> addVue() {
        Map<String,Object> mmap = new HashMap<>();
        SysUser user = getSysUser();
        SysDept sysDept = deptService.selectDeptById(user.getDeptId());
        mmap.put("user", user);
        mmap.put("dept", sysDept);
        mmap.put("userlist", userService.selectLeaderListFirst(Math.toIntExact(sysDept.getDeptId()),null));
        return CommonResult.success(mmap);
    }

    /**
     * 新增保存【 出差】
     */
    @Log(title = "【 出差】", businessType = BusinessType.INSERT)
    @PostMapping("/addOutSide")
    @ResponseBody
    public  CommonResult<?> addOutSide(@RequestBody  Outside outside) {
        SysUser user = getSysUser();
        return  CommonResult.success(outsideService.insertOutside(outside,user));
    }
}






